/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.veterinarySystem.bl;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import mx.its.dsw2.veterinarySystem.dao.DetalleExamenDaoLocal;
import mx.its.dsw2.veterinarySystem.dao.ExamenDaoLocal;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumTipoExamenLab;
import mx.its.dsw2.veterinarySystem.enumeraciones.TransactionStatus;
import mx.its.dsw2.veterinarySystem.modelo.DetalleExamen;
import mx.its.dsw2.veterinarySystem.modelo.Examen;
import mx.its.dsw2.veterinarySystem.modelo.Mascota;

/**
 *
 * @author 100101
 */
@Stateless
public class ExamenBl implements ExamenBlLocal {
    @EJB
    private DetalleExamenDaoLocal detalleExamenDao;
    @EJB
    private ExamenDaoLocal examenDao;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public TransactionStatus register(Examen examination,List<DetalleExamen> detail) {
        
        examination.setFecha(new Date());
        examination.setHora(new Time(new Date().getTime()));
        examenDao.create(examination);
        for(DetalleExamen detailExamination:detail){
        //DetalleExamen de=new DetalleExamen();
//        de.setCategoriaExamen(detailExamination.getCategoriaExamen());
//        de.setTipoExamen(detailExamination.getTipoExamen());
        detailExamination.setIdExamen(examination);
        detalleExamenDao.create(detailExamination);
        }
        examenDao.edit(examination);
        return TransactionStatus.REALIZED;
    }

    @Override
    public TransactionStatus modify(Examen examination, List<DetalleExamen> lista) {
        if(examination.getId()==null||examenDao.find(examination.getId())==null){
            return TransactionStatus.TRUNCATED;
        }else{
        examenDao.edit(examination);
      
            List<DetalleExamen> old=detalleExamenDao.getListByExam(examination);
            for(DetalleExamen c:old){
//                 System.out.println("id libro "+c.getIdLibro()+" lista vieja catttt "+old);
                detalleExamenDao.remove(c);
            }
            
            for(DetalleExamen c:lista){
//                DetalleExamen cdl=new DetalleExamen();
//                cdl.setIdExamen(examination);
               c.setIdExamen(examination);
                detalleExamenDao.create(c);
            }
        return TransactionStatus.REALIZED;
        }
    }

   
    @Override
    public List<Examen> getAll() {
        return examenDao.findAll();
    }

    @Override
    public List<Examen> findMascotByClave(String clave) {
//        if(clave!=null){
         return examenDao.findMascotByClave(clave);   
        
    }

    @Override
    public Examen getExamById(int idExam) {
        return examenDao.find(idExam);
    }

}
